#ifndef __SERVER_H__
#define __SERVER_H__

#include <soar.h>

__BEGIN_DECLS

/*----- 内部服务 -----*/
#define IN_TIMEOUT                  5       /* 内部超时 */
#define IN_SVC_NEW_SLOT             "0001"  /* 新连接 */

/*----- 共享信息 -----*/
#define WORKER_NUM                  8
#define SENDER_NUM                  4
#define SENDER_VIP_IDX              0

//typedef enum { _SHUTDOWN_=1, _SUSPENDED_, _RUNNING_ } _STAT;

struct black
{/*{{{*/
    char *name;
    struct rb_link link;
};/*}}}*/

struct share_info
{/*{{{*/
    char user[NAME_SIZE+1];
    struct
    {
        char location[LONG_FILE_NM_SIZE+1];
        unsigned int tracking;
    } _errlog_;
    struct
    {
        unsigned int server_port;
        unsigned int monitor_port;
    } _access_;
    struct
    {
        char black_list_file[LONG_FILE_NM_SIZE+1];
        char mail_store_dir[LONG_FILE_NM_SIZE+1];
        char smtp_addr[LONG_FILE_NM_SIZE+1];
        char smtp_addr_vip[LONG_FILE_NM_SIZE+1];
    } _mail_;

    int worker_num;
    int sender_num;

    pthread_mutex_t stat_lock;
    struct _worker_stat
    {
        int slot_used_num;
    } *worker_stat;
    struct _sender_stat
    {
        int send_num; 
        int todo_num; 
    } *sender_stat;

    struct rb_tree black_list;

    struct host_addr_info *smtp_ready;
    int smtp_ready_num;
    struct host_addr_info *smtp_backup;
    struct host_addr_info *smtp_ready_vip;
    int smtp_ready_vip_num;
    struct host_addr_info *smtp_backup_vip;

    pthread_mutex_t mail_id_lock;
    unsigned int mail_id;
    char curr_dt[DATE_SIZE+1];

    THRD_MSGQ *sender_msgq;
};/*}}}*/
extern struct share_info *share_info;

/*----- monitor -----*/
#define MONITOR_IDLE_TIMEOUT    SECOND_OF_MINUTE(5) /* 监控闲置超时 */

struct monitor_ctl
{/*{{{*/
    SKT skt;
//    SOCKET monitor_sock;
//    SOCKET listener_sock;
    char *msg_in;
    char *msg_out;
};/*}}}*/

extern int thrd_monitor(struct monitor_ctl *monitor_ctl);

/*----- listener -----*/
struct listener_ctl
{/*{{{*/
    unsigned long thrd_id;
    EP ep;
    //EP_SLOT monitor_slot;
    EP_SLOT listener_slot;
    EP_SLOT worker_slot[WORKER_NUM];
};/*}}}*/

extern void thrd_listener(struct listener_ctl *listener_ctl);

/*----- worker -----*/
struct worker_ctl
{/*{{{*/
    unsigned long thrd_id;
    int worker_idx;

    EP ep;
    EP_SLOT listener_slot;

    char *msg_in;
    char *msg_out;
};/*}}}*/

#define WORKER_SLOT_NUM     64  /* 工作线程通信槽数 */

extern void thrd_worker(struct worker_ctl *worker_ctl);

/*----- sender -----*/
struct sender_ctl
{/*{{{*/
    unsigned long thrd_id;
    int sender_idx;

    SMTP *smtp_ready;
    SMTP smtp_backup;
    struct listp_link msgq_todo;
};/*}}}*/

extern void thrd_sender(struct sender_ctl *sender_ctl);

#define MAIL_SEQ_SIZE       9   /* 邮件序列号长度 */
#define MAIL_FILENAME_SIZE   \
(DATE_SIZE+1+MAIL_SEQ_SIZE+strlen(".txt"))
#define MAIL_VIP_FILENAME_SIZE   \
(DATE_SIZE+1+MAIL_SEQ_SIZE+strlen("_vip.txt"))

struct mail
{/*{{{*/
    int try_times;
    char file[FILE_NM_SIZE+1];
    char rcpt[LONG_FILE_NM_SIZE+1];
    char from[FILE_NM_SIZE+1];
    char reply[FILE_NM_SIZE+1];
    char subject[LONG_FILE_NM_SIZE+1];
    char *body;
    struct listp_link link;
};/*}}}*/

__END_DECLS

#endif
